package com.zzyl.nursing.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;

/**
 * 合同Service业务层处理
 * 
 * @author lapalace
 * @date 2025-07-26
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper,Contract> implements IContractService
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同
     * 
     * @param id 合同主键
     * @return 合同
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectById(id);
    }

    /**
     * 查询合同列表
     * 
     * @param contract 合同
     * @return 合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        contract.setCreateTime(DateUtils.getNowDate());
        return contractMapper.insert(contract);
    }

    /**
     * 修改合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        contract.setUpdateTime(DateUtils.getNowDate());
        return contractMapper.updateById(contract);
    }

    /**
     * 批量删除合同
     * 
     * @param ids 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteByIds(Arrays.asList(ids));
    }

    /**
     * 删除合同信息
     * 
     * @param id 合同主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return contractMapper.deleteById(id);
    }

    //@Scheduled(cron =" 0 0 1 * * ?")
    //说白了，这就是个查询的增强
    //增强方式就是定时周期性执行
    //且 查询所有contract list all
    //对每个合同进行遍历，检验状态，状态根据过期不过期而定，
    //如果开始时间大于当前时间，合同未开始，状态为0
    //如果结束时间小于当前时间，合同已经结束，状态为0
    //如果结束时间小于
    @Scheduled(cron =" 0 0 1 * * ?")
    public void checkContractStatus(){
        LambdaQueryWrapper<Contract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Contract::getStatus,1);

        List<Contract> contracts = list(queryWrapper);
        LocalDateTime today = LocalDateTime.now();
        Date now = new Date();
        int updatedCount = 0;

        for(Contract contract : contracts) {
            if(contract.getStartDate()!=null && contract.getStartDate().isAfter(today)){
                contract.setStatus(0);
                contract.setUpdateTime(now);

                boolean updated = updateById(contract);
                if(updated){
                    updatedCount++;
                }
            }
        }
    }
}
